package com.hmall.controller.back;

import com.hdmall.business.common.annotation.LoginRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.*;
import com.hmall.model.*;
import com.hmall.util.TokenUtil;
import com.hmall.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

@RestController
@RequestMapping("/manage/xtx")
@Api(value = "纤天下管理操作API", description = "纤天下管理类API说明")
public class XtxArticleController {
    private final XtxArticleMapper xtxArticleMapper;

    private final XtxUserInfoMapper xtxUserInfoMapper;

    private final XtxArticleCommentMapper xtxArticleCommentMapper;

    private final XtxUserLikeMapper xtxUserLikeMapper;

    private final SysUserMapper sysUserMapper;

    private final XtxCommentDetailMapper xtxCommentDetailMapper;

    private final XtxArticleReaderMapper xtxArticleReaderMapper;

    private final XtxUserFocusMapper xtxUserFocusMapper;

    private final XtxArticleTypeMapper xtxArticleTypeMapper;

    private final XtxUserTypeMapper xtxUserTypeMapper;

    private final
    JedisPool jedisPool;

    private TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public XtxArticleController(JedisPool jedisPool, XtxArticleMapper xtxArticleMapper, XtxUserInfoMapper xtxUserInfoMapper, XtxArticleCommentMapper xtxArticleCommentMapper, XtxUserLikeMapper xtxUserLikeMapper, SysUserMapper sysUserMapper, XtxCommentDetailMapper xtxCommentDetailMapper, XtxArticleReaderMapper xtxArticleReaderMapper, XtxUserFocusMapper xtxUserFocusMapper, XtxArticleTypeMapper xtxArticleTypeMapper, XtxUserTypeMapper xtxUserTypeMapper) {
        this.xtxArticleMapper = xtxArticleMapper;
        this.jedisPool = jedisPool;
        this.xtxUserInfoMapper = xtxUserInfoMapper;
        this.xtxArticleCommentMapper = xtxArticleCommentMapper;
        this.xtxUserLikeMapper = xtxUserLikeMapper;
        this.sysUserMapper = sysUserMapper;
        this.xtxCommentDetailMapper = xtxCommentDetailMapper;
        this.xtxArticleReaderMapper = xtxArticleReaderMapper;
        this.xtxUserFocusMapper = xtxUserFocusMapper;
        this.xtxArticleTypeMapper = xtxArticleTypeMapper;
        this.xtxUserTypeMapper = xtxUserTypeMapper;
    }

    //发布文章相关接口
    @LoginRequired
    @GetMapping("getMyFocus")
    @ApiOperation(value = "获取我的关注")
    public ServerResponse getMyFocus(String token, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        total = xtxUserFocusMapper.selectCountByUserId(user.getUserId());
        List<XtxUserInfoVo> list = new ArrayList<>();
        List<XtxUserInfo> list1 = xtxUserInfoMapper.selectListAllByUserId(user.getUserId(),pageStart,pageSize);
        for (XtxUserInfo xtxUserInfo:list1){
            XtxUserInfoVo xtxUserInfoVo = new XtxUserInfoVo();
            BeanUtils.copyProperties(xtxUserInfo,xtxUserInfoVo);
            XtxUserFocus xtxUserFocus = xtxUserFocusMapper.selectByUserIdAndFocusUserId(xtxUserInfo.getUserId(),user.getUserId());
            Integer fansCount = xtxUserFocusMapper.selectFansCountByUserId(xtxUserInfo.getUserId());
            xtxUserInfoVo.setFansCount(fansCount);
            xtxUserInfoVo.setStatus(1);
            if (xtxUserFocus == null||xtxUserFocus.getStatus()==0){
                xtxUserInfoVo.setFansStatus(0);
            }else {
                xtxUserInfoVo.setFansStatus(1);
            }

            list.add(xtxUserInfoVo);
        }
        PageVo<XtxUserInfoVo> pageVo = new PageVo<>(pageNum, pageSize, total, list);
        return ServerResponse.createBySuccess(pageVo);
    }



    //以下为4.22之前接口

    @GetMapping("checkUserInfo")
    @ApiOperation(value = "校验用户信息")
    public ServerResponse checkUserInfo(String token) throws Exception {
        SysUser user;
        user = getSysUser(token);
        if (user == null){
            return ServerResponse.createBySuccess("未登录用户，不需要校验");
        }
        XtxUserInfo xtxUserInfo = xtxUserInfoMapper.selectByUserId(user.getUserId());
        if (xtxUserInfo==null){
            return ServerResponse.createByErrorMessage("未完善个人信息");
        }else {
            return ServerResponse.createBySuccess("已完善个人信息");
        }
    }

    private SysUser getSysUser(String token) throws Exception {
        SysUser user = null;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            jedis.close();
        }
        return user;
    }

    @GetMapping("searchUserInCategoryPage")
    @ApiOperation(value = "分类页面搜索用户")
    public ServerResponse searchUserInCategoryPage(String token,String keyword,Integer userTypeId, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
        SysUser user;
        user = getSysUser(token);
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = xtxUserInfoMapper.selectCountByKeywordAndType(keyword,userTypeId);
        List<XtxUserInfo> list = xtxUserInfoMapper.selectByKeywordAndType(keyword,userTypeId,pageStart,pageSize);
        List<XtxUserInfoVo> list1 = new ArrayList<>();
        searchUserAddDetail(user, list, list1);
        PageVo<XtxUserInfoVo> pageVo = new PageVo<>(pageNum, pageSize, total, list1);
        return ServerResponse.createBySuccess(pageVo);
    }

    private void searchUserAddDetail(SysUser user, List<XtxUserInfo> list, List<XtxUserInfoVo> list1) {
        for (XtxUserInfo xtxUserInfo:list){
            XtxUserInfoVo xtxUserInfoVo = new XtxUserInfoVo();
            BeanUtils.copyProperties(xtxUserInfo,xtxUserInfoVo);
            if (user!=null){
                XtxUserFocus xtxUserFocus = xtxUserFocusMapper.selectByUserIdAndFocusUserId(user.getUserId(),xtxUserInfo.getUserId());
                if (xtxUserFocus == null||xtxUserFocus.getStatus()==0){
                    xtxUserInfoVo.setStatus(0);
                }else {
                    xtxUserInfoVo.setStatus(1);
                }
            }else {
                xtxUserInfoVo.setStatus(0);
            }
            Integer fansCount = xtxUserFocusMapper.selectFansCountByUserId(xtxUserInfo.getUserId());
            xtxUserInfoVo.setFansCount(fansCount);
            addFansStatus(user, xtxUserInfo, xtxUserInfoVo);

            list1.add(xtxUserInfoVo);
        }
    }

    private void addFansStatus(SysUser user, XtxUserInfo xtxUserInfo, XtxUserInfoVo xtxUserInfoVo) {
        if (user == null){
            xtxUserInfoVo.setFansStatus(0);
        }else {
            XtxUserFocus xtxUserFocus = xtxUserFocusMapper.selectByUserIdAndFocusUserId(xtxUserInfo.getUserId(),user.getUserId());
            if (xtxUserFocus==null||xtxUserFocus.getStatus()==0){
                xtxUserInfoVo.setFansStatus(0);
            }else {
                xtxUserInfoVo.setFansStatus(1);
            }
        }
    }

    @GetMapping("searchUser")
    @ApiOperation(value = "搜索用户")
    public ServerResponse searchUser(String token,String keyword, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
        SysUser user;
        user = getSysUser(token);
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = xtxUserInfoMapper.selectCountByKeyword(keyword);
        List<XtxUserInfo> list = xtxUserInfoMapper.selectByKeyword(keyword,pageStart,pageSize);
        List<XtxUserInfoVo> list1 = new ArrayList<>();
        searchUserAddDetail(user, list, list1);
        PageVo<XtxUserInfoVo> pageVo = new PageVo<>(pageNum, pageSize, total, list1);
        return ServerResponse.createBySuccess(pageVo);
    }

    @LoginRequired
    @GetMapping("getMyFans")
    @ApiOperation(value = "获取我的粉丝")
    public ServerResponse getMyFans(String token, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize)throws Exception{
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        total = xtxUserFocusMapper.selectFansCountByUserId(user.getUserId());
        List<XtxUserInfoVo> list = new ArrayList<>();
        List<XtxUserInfo> list1 = xtxUserInfoMapper.selectFansByUserId(user.getUserId(),pageStart,pageSize);
        addFansDetail(user, list, list1);
        PageVo<XtxUserInfoVo> pageVo = new PageVo<>(pageNum, pageSize, total, list);
        return ServerResponse.createBySuccess(pageVo);
    }

    private void addFansDetail(SysUser user, List<XtxUserInfoVo> list, List<XtxUserInfo> list1) {
        for (XtxUserInfo xtxUserInfo:list1){
            XtxUserInfoVo xtxUserInfoVo = new XtxUserInfoVo();
            BeanUtils.copyProperties(xtxUserInfo,xtxUserInfoVo);
            XtxUserFocus xtxUserFocus = xtxUserFocusMapper.selectByUserIdAndFocusUserId(user.getUserId(),xtxUserInfo.getUserId());
            Integer fansCount = xtxUserFocusMapper.selectFansCountByUserId(xtxUserInfo.getUserId());
            xtxUserInfoVo.setFansCount(fansCount);
            if (xtxUserFocus == null||xtxUserFocus.getStatus()==0){
                xtxUserInfoVo.setStatus(0);
            }else {
                xtxUserInfoVo.setStatus(1);
            }
            xtxUserInfoVo.setFansStatus(1);
            list.add(xtxUserInfoVo);
        }
    }

    @GetMapping("refreshRecommendUser")
    @ApiOperation(value = "刷新推荐用户")
    public ServerResponse refreshRecommendUser(String token) throws Exception{
        SysUser user;
        List<XtxUserInfoVo> list1 = new ArrayList();
        List<XtxUserInfo> list;
        user = getSysUser(token);
        if (user == null){
            //如果没有登陆
            list = xtxUserInfoMapper.selectByRand(5);

        }else {
            //如果已登录
            list = xtxUserInfoMapper.selectByRandAndUserId(5,user.getUserId());
        }
        addRecommendUserDetail(user, list1, list);
        return ServerResponse.createBySuccess(list1);
    }

    private void addRecommendUserDetail(SysUser user, List<XtxUserInfoVo> list1, List<XtxUserInfo> list) {
        for (XtxUserInfo xtxUserInfo:list){
            XtxUserInfoVo xtxUserInfoVo = new XtxUserInfoVo();
            BeanUtils.copyProperties(xtxUserInfo,xtxUserInfoVo);
            xtxUserInfoVo.setStatus(0);
            Integer fansCount = xtxUserFocusMapper.selectFansCountByUserId(xtxUserInfo.getUserId());
            xtxUserInfoVo.setFansCount(fansCount);
            addFansStatus(user, xtxUserInfo, xtxUserInfoVo);

            list1.add(xtxUserInfoVo);
        }
    }

    @GetMapping("focusHomePage")
    @ApiOperation(value = "关注首页")
    public ServerResponse focusHomePage(String token) throws Exception {
        SysUser user;
        List<XtxUserInfoVo> list1 = new ArrayList();
        List<XtxUserInfoVo> list2 = new ArrayList();
        List<XtxUserInfo> list;
        user = getSysUser(token);
        if (user == null){
            //如果没有登陆
            list = xtxUserInfoMapper.selectByRand(5);

        }else {
            //如果已登录
            list = xtxUserInfoMapper.selectByRandAndUserId(5,user.getUserId());
            Calendar cal = Calendar.getInstance();
            Date endTime = cal.getTime();
            cal.add(Calendar.DAY_OF_MONTH,-1);
            Date startTime = cal.getTime();
            List<XtxUserInfo> list3 = xtxUserInfoMapper.selectRecentFocusUserInfo(user.getUserId(),startTime,endTime);
            addRecentUserDetail(user, list2, list3);
        }
        addRecommendUserDetail(user, list1, list);
        XtxReturnVo xtxReturnVo = new XtxReturnVo();
        xtxReturnVo.setRecommendUserForYou(list1);
        xtxReturnVo.setRecentFocus(list2);
        xtxReturnVo.setRecommendArticleForYou(new ArrayList());
        xtxReturnVo.setFocusUserArticle(new ArrayList());
        return ServerResponse.createBySuccess(xtxReturnVo);
    }

    private void addRecentUserDetail(SysUser user, List<XtxUserInfoVo> list2, List<XtxUserInfo> list3) {
        for (XtxUserInfo xtxUserInfo:list3){
            XtxUserInfoVo xtxUserInfoVo = new XtxUserInfoVo();
            BeanUtils.copyProperties(xtxUserInfo,xtxUserInfoVo);
            xtxUserInfoVo.setStatus(0);
            Integer fansCount = xtxUserFocusMapper.selectFansCountByUserId(xtxUserInfo.getUserId());
            xtxUserInfoVo.setFansCount(fansCount);
            XtxUserFocus xtxUserFocus = xtxUserFocusMapper.selectByUserIdAndFocusUserId(xtxUserInfo.getUserId(),user.getUserId());
            if (xtxUserFocus==null||xtxUserFocus.getStatus()==0){
                xtxUserInfoVo.setFansStatus(0);
            }else {
                xtxUserInfoVo.setFansStatus(1);
            }
            list2.add(xtxUserInfoVo);
        }
    }
    @LoginRequired
    @GetMapping("addFocusPage")
    @ApiOperation(value = "添加关注页面")
    public ServerResponse addFocusPage(String token) throws Exception {
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        List<XtxUserInfoVo> list = new ArrayList<>();
        List<XtxUserInfo> list1 = xtxUserInfoMapper.selectFansByUserId(user.getUserId(),0,3);
        addFansDetail(user, list, list1);
        Integer fansCount = xtxUserFocusMapper.selectFansCountByUserId(user.getUserId());
        List<XtxUserType> list2 = xtxUserTypeMapper.selectAllType();

        List<XtxUserInfo> list3 = xtxUserInfoMapper.selectByRandAndUserId(3,user.getUserId());
        List<XtxUserInfoVo> list4 = new ArrayList<>();
        addRecentUserDetail(user, list4, list3);

        XtxReturnVo xtxReturnVo = new XtxReturnVo();
        xtxReturnVo.setFans(list);
        xtxReturnVo.setFansCount(fansCount);
        xtxReturnVo.setUserCategory(list2);
        xtxReturnVo.setRecommendUserForYou(list4);
        xtxReturnVo.setCategoryUserMap(new HashMap());
        xtxReturnVo.setRecentAccess(new ArrayList<>());
        return ServerResponse.createBySuccess(xtxReturnVo);
    }

    @PostMapping("addXtxArticle")
    @ApiOperation(value = "添加纤天下文章")
    public ServerResponse addXtxArticle(@RequestBody XtxArticleVo xtxArticleVo) throws Exception {
        String token = xtxArticleVo.getToken();
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage(" ");
        }
        XtxArticle xtxArticle = new XtxArticle();
        xtxArticle.setAuthorUserId(user.getUserId());
        xtxArticle.setTitle(xtxArticleVo.getTitle());
        xtxArticle.setSubTitle(xtxArticleVo.getSubTitle());
        xtxArticle.setContent(xtxArticleVo.getContent());
        xtxArticle.setTypeId(xtxArticleVo.getTypeId());
        xtxArticle.setUserId(user.getUserId());
        xtxArticle.setIsPublic(xtxArticleVo.getIsPublic());
        xtxArticle.setIsForwarding(0);
        xtxArticle.setIsTop(0);
        xtxArticle.setStatus(1);
        xtxArticle.setImage(xtxArticleVo.getImage());
        xtxArticle.setCreateTime(new Date());
        xtxArticleMapper.insert(xtxArticle);
        return ServerResponse.createBySuccess("添加成功");
    }

    @GetMapping("getXtxArticleDetail")
    @ApiOperation(value = "获取纤天下文章")
    public ServerResponse getXtxArticleDetail(Integer articleId, String token) throws Exception {
        XtxArticle xtxArticle = xtxArticleMapper.selectByPrimaryKey(articleId);
        if (xtxArticle == null) {
            return ServerResponse.createByErrorMessage("文章不存在");
        }
        if (xtxArticle.getStatus() == 0) {
            return ServerResponse.createByErrorMessage("该文章已被删除");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("article", xtxArticle);
        XtxUserInfo xtxUserInfo = xtxUserInfoMapper.selectByUserId(xtxArticle.getUserId());
        XtxUserInfo xtxUserInfo2 = xtxUserInfoMapper.selectByUserId(xtxArticle.getAuthorUserId());
        if (xtxUserInfo == null) {
            SysUser sysUser = sysUserMapper.selectByPrimaryKey(xtxArticle.getUserId());
            XtxUserInfo xtxUserInfo1 = new XtxUserInfo();
            xtxUserInfo1.setUserId(xtxArticle.getUserId());
            xtxUserInfo1.setUsername("手机用户" + sysUser.getUsername());
            xtxUserInfo1.setUserDesc("暂未设置");
            xtxUserInfo1.setCertification("暂未设置");
            xtxUserInfo1.setIcon(sysUser.getAvatar());
            map.put("userInfo", xtxUserInfo1);
        } else {
            map.put("userInfo", xtxUserInfo);
        }
        if (xtxUserInfo2 == null) {
            SysUser sysUser = sysUserMapper.selectByPrimaryKey(xtxArticle.getUserId());
            XtxUserInfo xtxUserInfo1 = new XtxUserInfo();
            xtxUserInfo1.setUserId(xtxArticle.getUserId());
            xtxUserInfo1.setUsername("手机用户" + sysUser.getUsername());
            xtxUserInfo1.setUserDesc("暂未设置");
            xtxUserInfo1.setCertification("暂未设置");
            xtxUserInfo1.setIcon(sysUser.getAvatar());
            map.put("authorInfo", xtxUserInfo1);
        } else {
            map.put("authorInfo", xtxUserInfo2);
        }
        Integer commentNum = xtxArticleCommentMapper.selectCountByArticleId(articleId);
        map.put("commentNum", commentNum);
        Integer likeNum = xtxUserLikeMapper.selectCountByTypeAndLikeId(0, articleId);
        map.put("likeNum", likeNum);
        Integer forwardingNum = xtxArticleMapper.selectForwardingCountByArticleId(articleId);
        map.put("forwardingNum", forwardingNum);
        //添加阅读数
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            SysUser user = serverResponse.getData();
            if (user != null) {
                XtxArticleReader xtxArticleReader = xtxArticleReaderMapper.selectByUserIdAndArticleId(user.getUserId(), articleId);
                if (xtxArticleReader == null) {
                    XtxArticleReader xtxArticleReader1 = new XtxArticleReader();
                    xtxArticleReader1.setArticleId(articleId);
                    xtxArticleReader1.setCreateTime(new Date());
                    xtxArticleReader1.setUserId(user.getUserId());
                    xtxArticleReaderMapper.insert(xtxArticleReader1);
                }
            }
            jedis.close();
        }
        return ServerResponse.createBySuccess(map);
    }

    @GetMapping("getArticleComment")
    @ApiOperation(value = "获取文章评论")
    public ServerResponse<PageVo<XtxCommentVo>> getArticleComment(Integer articleId, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = xtxArticleCommentMapper.selectCountByArticleId(articleId);
        List<XtxCommentVo> result = new ArrayList<>();
        List<XtxArticleComment> list = xtxArticleCommentMapper.selectByArticleId(articleId, pageStart, pageSize);
        for (XtxArticleComment xtxArticleComment : list) {
            XtxCommentVo xtxCommentVo = new XtxCommentVo();
            BeanUtils.copyProperties(xtxArticleComment, xtxCommentVo);
            XtxUserInfo xtxUserInfo = xtxUserInfoMapper.selectByUserId(xtxArticleComment.getUserId());
            if (xtxUserInfo == null) {
                SysUser sysUser = sysUserMapper.selectByPrimaryKey(xtxArticleComment.getUserId());
                xtxCommentVo.setIcon(sysUser.getAvatar());
                xtxCommentVo.setName("手机用户" + sysUser.getUsername());
            } else {
                xtxCommentVo.setName(xtxUserInfo.getUsername());
                xtxCommentVo.setIcon(xtxUserInfo.getIcon());
            }
            Integer commentNum = xtxCommentDetailMapper.selectCountByCommentId(xtxArticleComment.getArticleCommentId());
            xtxCommentVo.setCommentNum(commentNum);
            Integer likeNum = xtxUserLikeMapper.selectCountByTypeAndLikeId(1, xtxArticleComment.getArticleCommentId());
            xtxCommentVo.setLikeNum(likeNum);
            result.add(xtxCommentVo);
        }
        PageVo<XtxCommentVo> pageVo = new PageVo<>(pageNum, pageSize, total, result);
        return ServerResponse.createBySuccess(pageVo);
    }

    @GetMapping("getCommentDetail")
    @ApiOperation(value = "获取评论详情")
    public ServerResponse<PageVo<CommentDetailVo>> getCommentDetail(Integer articleCommentId, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = xtxCommentDetailMapper.selectCountByCommentId(articleCommentId);
        List<XtxCommentDetail> list = xtxCommentDetailMapper.selectByCommentId(articleCommentId, pageStart, pageSize);
        List<CommentDetailVo> list1 = new ArrayList<>();
        for (XtxCommentDetail xtxCommentDetail : list) {
            CommentDetailVo commentDetailVo = new CommentDetailVo();
            BeanUtils.copyProperties(xtxCommentDetail, commentDetailVo);
            Integer likeNum = xtxUserLikeMapper.selectCountByTypeAndLikeId(2, articleCommentId);
            commentDetailVo.setLikeNum(likeNum);
            XtxUserInfo xtxUserInfo = xtxUserInfoMapper.selectByUserId(xtxCommentDetail.getUserId());
            if (xtxUserInfo == null) {
                SysUser sysUser = sysUserMapper.selectByPrimaryKey(xtxCommentDetail.getUserId());
                commentDetailVo.setIcon(sysUser.getAvatar());
                commentDetailVo.setName("手机用户" + sysUser.getUsername());
            } else {
                commentDetailVo.setName(xtxUserInfo.getUsername());
                commentDetailVo.setIcon(xtxUserInfo.getIcon());
            }
            XtxUserInfo xtxUserInfo2 = xtxUserInfoMapper.selectByUserId(xtxCommentDetail.getReplyUserId());
            if (xtxUserInfo2 == null) {
                SysUser sysUser = sysUserMapper.selectByPrimaryKey(xtxCommentDetail.getReplyUserId());
                commentDetailVo.setReplyName("手机用户" + sysUser.getUsername());
            } else {
                commentDetailVo.setReplyName(xtxUserInfo2.getUsername());
            }
            list1.add(commentDetailVo);
        }
        PageVo<CommentDetailVo> pageVo = new PageVo<>(pageNum, pageSize, total, list1);
        return ServerResponse.createBySuccess(pageVo);
    }

    @LoginRequired
    @PostMapping("addArticleComment")
    @ApiOperation(value = "添加文章评论")
    public ServerResponse addArticleComment(@RequestBody CommentVo commentVo) throws Exception {
        String token = commentVo.getToken();
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage(" ");
        }
        XtxArticle xtxArticle = xtxArticleMapper.selectByPrimaryKey(commentVo.getArticleId());
        if (xtxArticle == null) {
            return ServerResponse.createByErrorMessage("文章不存在");
        }
        if (xtxArticle.getStatus() == 0) {
            return ServerResponse.createByErrorMessage("文章已被删除");
        }
        XtxArticleComment xtxArticleComment = new XtxArticleComment();
        xtxArticleComment.setComment(commentVo.getComment());
        xtxArticleComment.setArticleId(commentVo.getArticleId());
        xtxArticleComment.setCreateTime(new Date());
        xtxArticleComment.setStatus(1);
        xtxArticleComment.setUserId(user.getUserId());
        xtxArticleCommentMapper.insert(xtxArticleComment);
        return ServerResponse.createBySuccess("创建成功");
    }

    @LoginRequired
    @PostMapping("addCommentDetail")
    @ApiOperation(value = "添加评论详情")
    public ServerResponse addCommentDetail(@RequestBody CommentDetailVo2 commentDetailVo2) throws Exception {
        String token = commentDetailVo2.getToken();
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage(" ");
        }
        XtxArticleComment xtxArticleComment = xtxArticleCommentMapper.selectByPrimaryKey(commentDetailVo2.getArticleCommentId());
        if (xtxArticleComment == null) {
            return ServerResponse.createByErrorMessage("评论不存在");
        }
        if (xtxArticleComment.getStatus() == 0) {
            return ServerResponse.createByErrorMessage("评论已被删除");
        }
        XtxCommentDetail xtxCommentDetail = new XtxCommentDetail();
        xtxCommentDetail.setComment(commentDetailVo2.getComment());
        xtxCommentDetail.setCreateTime(new Date());
        xtxCommentDetail.setUserId(user.getUserId());
        xtxCommentDetail.setCommentId(commentDetailVo2.getArticleCommentId());
        xtxCommentDetail.setReplyUserId(commentDetailVo2.getReplyUserId());
        xtxCommentDetail.setStatus(1);
        xtxCommentDetailMapper.insert(xtxCommentDetail);
        return ServerResponse.createBySuccess("创建成功");
    }

    @LoginRequired
    @DeleteMapping("deleteArticle")
    @ApiOperation(value = "删除文章")
    public ServerResponse deleteArticle(Integer articleId, String token) throws Exception {
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        XtxArticle xtxArticle = xtxArticleMapper.selectByPrimaryKey(articleId);
        if (xtxArticle == null) {
            return ServerResponse.createByErrorMessage("文章不存在");
        }
        if (!xtxArticle.getUserId().equals(user.getUserId())) {
            return ServerResponse.createByErrorMessage("不是您的文章，无法删除");
        }
        if (xtxArticle.getStatus() == 0) {
            return ServerResponse.createByErrorMessage("文章已经被删除");
        }
        xtxArticle.setStatus(0);
        xtxArticleMapper.updateByPrimaryKey(xtxArticle);
        return ServerResponse.createBySuccess("删除成功");
    }

    @LoginRequired
    @PostMapping("AddOrCancelLike")
    @ApiOperation(value = "点赞/取消点赞", notes = "0: 文章  1: 评论  2: 评论详情")
    public ServerResponse AddLike(@RequestBody LikeVo likeVo) throws Exception {
        String token = likeVo.getToken();
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage(" ");
        }
        XtxUserLike xtxUserLike = xtxUserLikeMapper.selectByUserIdAndTypeAndLikeId(user.getUserId(), likeVo.getType(), likeVo.getId());
        if (xtxUserLike == null) {
            //没有新建
            XtxUserLike xtxUserLike1 = new XtxUserLike();
            xtxUserLike1.setStatus(1);
            xtxUserLike1.setCreateTime(new Date());
            xtxUserLike1.setLikeId(likeVo.getId());
            xtxUserLike1.setUserId(user.getUserId());
            xtxUserLike1.setType(likeVo.getType());
            xtxUserLikeMapper.insert(xtxUserLike1);
        } else {
            //更改状态
            if (xtxUserLike.getStatus() == 0) {
                xtxUserLike.setStatus(1);
            } else {
                xtxUserLike.setStatus(0);
            }
            xtxUserLikeMapper.updateByPrimaryKey(xtxUserLike);
        }
        return ServerResponse.createBySuccess("修改成功");
    }

    @LoginRequired
    @GetMapping("getXtxInfo")
    @ApiOperation(value = "获取纤天下个人信息")
    public ServerResponse getXtxInfo(String token) throws Exception {
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        Map<String, Object> map = new HashMap<>();
        XtxUserInfo xtxUserInfo = xtxUserInfoMapper.selectByUserId(user.getUserId());
        if (xtxUserInfo == null) {
            SysUser sysUser = sysUserMapper.selectByUserId(user.getUserId());
            XtxUserInfo xtxUserInfo1 = new XtxUserInfo();
            xtxUserInfo1.setIcon(sysUser.getAvatar());
            xtxUserInfo1.setUsername("手机用户" + sysUser.getUsername());
            xtxUserInfo1.setUserDesc("暂未设置");
            xtxUserInfo1.setCertification("暂未设置");
            xtxUserInfo1.setUserId(user.getUserId());
            map.put("userInfo", xtxUserInfo1);
        } else {
            map.put("userInfo", xtxUserInfo);
        }
        //获得文章点赞数
        Integer likeNum1 = xtxUserLikeMapper.selectArticleCountByUserId(user.getUserId());
        //获得评论点赞数
        Integer likeNum2 = xtxUserLikeMapper.selectCommentCountByUserId(user.getUserId());
        //获得评论详情点赞数
        Integer likeNum3 = xtxUserLikeMapper.selectCommentDetailCountByUserId(user.getUserId());
        map.put("likeNum", likeNum1 + likeNum2 + likeNum3);
        Integer readNum = xtxArticleReaderMapper.selectCountByUserId(user.getUserId());
        map.put("readNum", readNum);
        Integer articleNum = xtxArticleMapper.selectCountByUserId(user.getUserId());
        map.put("articleNum", articleNum);
        Integer focusNum = xtxUserFocusMapper.selectCountByUserId(user.getUserId());
        map.put("focusNum", focusNum);
        Integer fansNum = xtxUserFocusMapper.selectFansCountByUserId(user.getUserId());
        map.put("fansNum", fansNum);
        return ServerResponse.createBySuccess(map);
    }

    @LoginRequired
    @GetMapping("focusUser")
    @ApiOperation(value = "关注或取关用户")
    public ServerResponse focusUser(String token, Integer userId) throws Exception {
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        if (user.getUserId().equals(userId)) {
            return ServerResponse.createByErrorMessage("不能自己关注自己");
        }
        XtxUserFocus xtxUserFocus = xtxUserFocusMapper.selectByUserIdAndFocusUserId(user.getUserId(), userId);
        if (xtxUserFocus == null) {
            XtxUserFocus xtxUserFocus1 = new XtxUserFocus();
            xtxUserFocus1.setCreateTime(new Date());
            xtxUserFocus1.setFocusUserId(userId);
            xtxUserFocus1.setUserId(user.getUserId());
            xtxUserFocus1.setStatus(1);
            xtxUserFocusMapper.insert(xtxUserFocus1);
        } else {
            if (xtxUserFocus.getStatus() == 0) {
                xtxUserFocus.setStatus(1);
            } else {
                xtxUserFocus.setStatus(0);
            }
            xtxUserFocusMapper.updateByPrimaryKey(xtxUserFocus);
        }
        return ServerResponse.createBySuccess("修改成功");

    }

    @GetMapping("getHomeArticle")
    @ApiOperation(value = "获取主页文章")
    public ServerResponse<PageVo<ArticleVo>> getHomeArticle(String keyword, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = xtxArticleMapper.selectCountByKeyword(keyword);
        List<XtxArticle> list = xtxArticleMapper.selectByKeyWord(keyword, pageStart, pageSize);
        List<ArticleVo> result = new ArrayList<>();
        addArticleDetail(list, result);
        PageVo<ArticleVo> pageVo = new PageVo<>(pageNum, pageSize, total, result);
        return ServerResponse.createBySuccess(pageVo);
    }

    @LoginRequired
    @PostMapping("updateXtxInfo")
    @ApiOperation(value = "修改纤天下个人信息")
    public ServerResponse updateXtxInfo(@RequestBody XtxUserInfoVo xtxUserInfoVo) throws Exception {
        String token = xtxUserInfoVo.getToken();
        SysUser user;
        if (token != null && !token.equals("")) {
            Jedis jedis = jedisPool.getResource();
            ServerResponse<SysUser> serverResponse = tokenUtil.tokenToUser(jedis, token);
            user = serverResponse.getData();
            if (user == null) {
                return serverResponse;
            }
            jedis.close();
        } else {
            return ServerResponse.createByErrorMessage(" ");
        }
        XtxUserInfo xtxUserInfo = xtxUserInfoMapper.selectByUserId(user.getUserId());
        if (xtxUserInfo == null) {
            XtxUserInfo xtxUserInfo1 = new XtxUserInfo();
            xtxUserInfo1.setUserId(user.getUserId());
            xtxUserInfo1.setUsername(xtxUserInfoVo.getUsername());
            xtxUserInfo1.setCertification(xtxUserInfoVo.getCertification());
            xtxUserInfo1.setUserDesc(xtxUserInfoVo.getUserDesc());
            xtxUserInfo1.setIcon(xtxUserInfoVo.getIcon());
            xtxUserInfo1.setCreateTime(new Date());
            xtxUserInfoMapper.insert(xtxUserInfo1);
        } else {
            xtxUserInfo.setUserDesc(xtxUserInfoVo.getUserDesc());
            xtxUserInfo.setUsername(xtxUserInfoVo.getUsername());
            xtxUserInfo.setCertification(xtxUserInfoVo.getCertification());
            xtxUserInfo.setIcon(xtxUserInfoVo.getIcon());
            xtxUserInfoMapper.updateByPrimaryKey(xtxUserInfo);
        }
        return ServerResponse.createBySuccess("修改成功");
    }

    @LoginRequired
    @GetMapping("getFocusArticle")
    @ApiOperation(value = "获取关注文章")
    public ServerResponse getFocusArticle(String keyword, String token, @RequestParam(value = "pageNum", defaultValue = "1")
            int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) throws Exception {
        Jedis jedis = jedisPool.getResource();
        SysUser user = tokenUtil.tokenToUser(jedis, token).getData();
        Integer pageStart = pageSize * (pageNum - 1);
        Integer total;
        total = xtxArticleMapper.selectFocusCountByKeyword(keyword, user.getUserId());
        List<XtxArticle> list = xtxArticleMapper.selectFocusArticleByKeyWord(keyword, user.getUserId(), pageStart, pageSize);
        List<ArticleVo> result = new ArrayList<>();
        addArticleDetail(list, result);
        PageVo<ArticleVo> pageVo = new PageVo<>(pageNum, pageSize, total, result);
        return ServerResponse.createBySuccess(pageVo);
    }

    private void addArticleDetail(List<XtxArticle> list, List<ArticleVo> result) {
        for (XtxArticle xtxArticle : list) {
            ArticleVo articleVo = new ArticleVo();
            articleVo.setCreateTime(xtxArticle.getCreateTime());
            articleVo.setArticleId(xtxArticle.getArticleId());
            articleVo.setTitle(xtxArticle.getTitle());
            XtxUserInfo xtxUserInfo = xtxUserInfoMapper.selectByUserId(xtxArticle.getUserId());
            if (xtxUserInfo == null) {
                SysUser sysUser = sysUserMapper.selectByUserId(xtxArticle.getUserId());
                articleVo.setName("手机用户" + sysUser.getUsername());
            } else {
                articleVo.setName(xtxUserInfo.getUsername());
            }
            articleVo.setUserId(xtxArticle.getUserId());
            Integer commentNum = xtxArticleCommentMapper.selectCountByArticleId(xtxArticle.getArticleId());
            articleVo.setCommentNum(commentNum);
            result.add(articleVo);
        }
    }

    @GetMapping("getArticleType")
    @ApiOperation(value = "获得文章类型")
    public ServerResponse<List<XtxArticleType>> getArticleType() {
        List<XtxArticleType> list = xtxArticleTypeMapper.selectMyAll();
        return ServerResponse.createBySuccess(list);
    }
}
